﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TheDealmap.Data
{
    class GeoCoord
    {
        private double _latitude;
        private double _longitude;
        private string numberformat = "+0.0#############;-0.0#############";            //string numberformat = "+0.0000000;-0.0000000";

        /// <summary>
        /// Initializes a new instance of the <see cref="GeoCoord"/> class.
        /// </summary>
        /// <param name="latitude">The latitude.</param>
        /// <param name="longitude">The longitude.</param>
        public GeoCoord(double latitude, double longitude)
        {
            Latitude = latitude;
            Longitude = longitude;
        }

        /// <summary>
        /// Gets or sets the latitude.
        /// </summary>
        /// <value>The latitude.</value>
        public double Latitude
        {
            get
            {
                return _latitude;
            }
            set
            {
                if (!this.CheckValid(value, 180))
                {
                    throw new ArgumentOutOfRangeException("Latitude must be between -180.00 and +180.00");
                }
                _latitude = value;
            }
        }
        /// <summary>
        /// Gets or sets the longitude.
        /// </summary>
        /// <value>The longitude.</value>
        public double Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                if(!this.CheckValid(value, 90))
                {
                    throw new ArgumentOutOfRangeException("Latitute must be between -90.0 +90.0");
                }
                _longitude = value;
            }
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var location = String.Format("{0}{1}",Latitude.ToString(numberformat),  Longitude.ToString(numberformat));
            return location;
        }

        private bool CheckValid(double value, double range)
        {
            range = (range < 0) ? range * -1 : range;
            return (value > -(range) || value < range);
        }
    }
}
